/**
 * Created by Administrator on 2017/12/7.
 */
$(function(){
  /*BEGIN 投票倒计时*/
  /*$.ajax({
   type: "get",
   async: false,
   dataType: 'json',
   url: '/v1/candidate/conf/info',
   data: JSON.stringify({}),
   "contentType": "application/json;charset=UTF-8",
   success: function(data){
     if(data.status == 200){
       var startTime = new Date(data.data.startTime).getTime();
       var endTime = new Date(data.data.endTime).getTime();
       var curTime = new Date().getTime();
       if(curTime < startTime){
         var clockStartTime = Math.floor((startTime - curTime)/1000);
         var clockDay,clockHour,clockMinute,clockSecond;
         var timer1 = setInterval(function(){
           clockStartTime --;
           clockDay = Math.floor(clockStartTime/86400);
           clockHour = Math.floor((clockStartTime%86400)/3600) < 10? '0' + Math.floor((clockStartTime%86400)/3600): Math.floor((clockStartTime%86400)/3600);
           clockMinute = Math.floor((clockStartTime%3600)/60) < 10? '0' + Math.floor((clockStartTime%3600)/60):Math.floor((clockStartTime%3600)/60);
           clockSecond = Math.floor(clockStartTime%60) < 10? '0' + Math.floor(clockStartTime%60): Math.floor(clockStartTime%60);
           $('.clock').html('<span>距离投票开始还有</span>' + '<i>' + clockDay + '</i>天' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
           if(clockStartTime <= 0){
             clearInterval(timer1);
             var clockEndTime = Math.floor((endTime - new Date().getTime())/1000);
             var timer2 = setInterval(function(){
               clockEndTime --;
               clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
               clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
               clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
               $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
               if(clockEndTime <= 0){
                 clearInterval(timer2);
                 $('.clock').html('<span>投票已结束</span>');
               }
             },1000);
           }
         },1000);
       }else if(curTime > endTime){
         $('.clock').html('<span>投票已结束</span>');
       }else{
         var clockEndTime = Math.floor((endTime - curTime)/1000);
         var timer3 = setInterval(function(){
           clockEndTime --;
           clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
           clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
           clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
           $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
           if(clockEndTime <= 0){
             clearInterval(timer3);
             $('.clock').html('<span>投票已结束</span>');
           }
         },1000);
       }
     }
   }
  });*/
  //暂时使用假数据代替，待删
  var data = {
    "msg": "ok",
    "data": {
      "startTime": "2018-1-28 09:00:00",
      "endTime": "2018-1-28 17:00:00"
    },
    "status": 200
  };
  if(data.status == 200){
    var startTime = new Date(data.data.startTime).getTime();
    var endTime = new Date(data.data.endTime).getTime();
    var curTime = new Date().getTime();
    if(curTime < startTime){
      var clockStartTime = Math.floor((startTime - curTime)/1000);
      var clockDay,clockHour,clockMinute,clockSecond;
      var timer1 = setInterval(function(){
        clockStartTime --;
        clockDay = Math.floor(clockStartTime/86400);
        clockHour = Math.floor((clockStartTime%86400)/3600) < 10? '0' + Math.floor((clockStartTime%86400)/3600): Math.floor((clockStartTime%86400)/3600);
        clockMinute = Math.floor((clockStartTime%3600)/60) < 10? '0' + Math.floor((clockStartTime%3600)/60):Math.floor((clockStartTime%3600)/60);
        clockSecond = Math.floor(clockStartTime%60) < 10? '0' + Math.floor(clockStartTime%60): Math.floor(clockStartTime%60);
        $('.clock').html('<span>距离投票开始还有</span>' + '<i>' + clockDay + '</i>天' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
        if(clockStartTime <= 0){
          clearInterval(timer1);
          var clockEndTime = Math.floor((endTime - new Date().getTime())/1000);
          var timer2 = setInterval(function(){
            clockEndTime --;
            clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
            clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
            clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
            $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
            if(clockEndTime <= 0){
              clearInterval(timer2);
              $('.clock').html('<span>投票已结束</span>');
            }
          },1000);
        }
      },1000);
    }else if(curTime > endTime){
      $('.clock').html('<span>投票已结束</span>');
    }else{
      var clockEndTime = Math.floor((endTime - curTime)/1000);
      var timer3 = setInterval(function(){
        clockEndTime --;
        clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
        clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
        clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
        $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
        if(clockEndTime <= 0){
          clearInterval(timer3);
          $('.clock').html('<span>投票已结束</span>');
        }
      },1000);
    }
  }
  /*END 投票倒计时*/

  /*BEGIN 左侧柱状图*/
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(document.getElementById('main-left'));
  // 指定图表的配置项和数据
  var option = {
    title: {
      text: '票数统计',
      textStyle: {
        color: '#3398DB',
        fontSize: 24,
        align: 'center',
        lineHeight: 24
      },
      x:'center',
      y:'top',
      textAlign:'left'
    },
    color: ['#3398DB'],
    tooltip : {
      trigger: 'axis',
      axisPointer : {            // 坐标轴指示器，坐标轴触发有效
        type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis : {
      type : 'category',
      axisLabel :{
        interval: 0,
        rotate: 45
      },
      data : [],
      axisTick: {
        alignWithLabel: true
      }
    },
    yAxis : [
      {
        type : 'value',
        name: '票数'
      }
    ],
    series : [
      {
        name:'获得票数',
        type:'bar',
        barWidth: '60%',
        data:[]
      }
    ],
    label: {
      normal: {
        show: true,
        position: 'top',
        color: '#333'
      }
    }
  };
  // 使用刚指定的配置项和数据显示图表。
  /*END 左侧柱状图*/

  /*BEGIN 获取获取候选人列表*/
  /*var getEmpList = function(wc){
    $.ajax({
      type: "get",
      async: false,
      dataType: 'json',
      url: '/v1/candidate/findlist',
      data: JSON.stringify({
        'wc': wc
      }),
      "contentType": "application/json;charset=UTF-8",
      success: function(data){
        if(data2.status == 200){
          //柱状图数据
          var empNameList = [];
          var empAmount = [];
          for(var i = 0; i < data2.data.length; i++){
            empNameList.push(data2.data[i].name);
            empAmount.push(data2.data[i].amount);
          }
          option.xAxis.data = empNameList;
          option.series[0].data = empAmount;
          myChart.setOption(option);
          //表格数据
          var flag = true;
          var temp = data2.data[0];
          for(var k = 0; k < data2.data.length - 1; k++){
            for(var j = 0; j < data2.data.length - 1 - k; j++){
              if(data2.data[j].amount < data2.data[j+1].amount){
                temp = data2.data[j];
                data2.data[j] = data2.data[j+1];
                data2.data[j+1] = temp;
                flag = false;
              }
            }
            if(flag){return;}
          }
          for(var l = 0; l < data2.data.length; l++){
            $('#voteTable>tr:nth-of-type('+ (l + 1) +')>td:nth-of-type(2)').text(data2.data[l].name);
            $('#voteTable>tr:nth-of-type('+ (l + 1) +')>td:nth-of-type(3)').text(data2.data[l].amount);
          }
        }
      }
    });
  };
  setInterval(function(){
    getEmpList(1);
  },5000);*/
  var data2 = {
    "msg": "ok",
    "data": [
      {
        "amount": 23,
        "headimg": "https://www.baidu.com/img/bd_logo1.png",
        "introduce": null,
        "name": "员工1",
        "empNo": "9001",
        "cid": 1
      },
      {
        "amount": 300,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工2",
        "empNo": "9002",
        "cid": 2
      },{
        "amount": 25,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工3",
        "empNo": "9003",
        "cid": 2
      },{
        "amount": 22,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工4",
        "empNo": "9004",
        "cid": 2
      },{
        "amount": 55,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工5",
        "empNo": "9005",
        "cid": 2
      },{
        "amount": 234,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工6",
        "empNo": "9006",
        "cid": 2
      },{
        "amount": 111,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工7",
        "empNo": "9007",
        "cid": 2
      },{
        "amount": 123,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工8",
        "empNo": "9008",
        "cid": 2
      },{
        "amount": 241,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工9",
        "empNo": "900",
        "cid": 2
      },{
        "amount": 153,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工10",
        "empNo": "90010",
        "cid": 2
      },{
        "amount": 53,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工11",
        "empNo": "90011",
        "cid": 2
      },{
        "amount": 222,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工12",
        "empNo": "90012",
        "cid": 2
      },{
        "amount": 352,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工13",
        "empNo": "90013",
        "cid": 2
      },{
        "amount": 135,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工14",
        "empNo": "90014",
        "cid": 2
      },{
        "amount": 222,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工15",
        "empNo": "90015",
        "cid": 2
      },{
        "amount": 356,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工16",
        "empNo": "90016",
        "cid": 2
      },{
        "amount": 441,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工17",
        "empNo": "90017",
        "cid": 2
      },{
        "amount": 222,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工18",
        "empNo": "90018",
        "cid": 2
      },{
        "amount": 321,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工19",
        "empNo": "90019",
        "cid": 2
      },{
        "amount": 225,
        "headimg": "https://www.baidu.com/img/bd_logo.png",
        "introduce": null,
        "name": "员工20",
        "empNo": "90020",
        "cid": 2
      }
    ],
    "status": 200
  };
  if(data2.status == 200){
    //柱状图数据
    var empNameList = [];
    var empAmount = [];
    for(var i = 0; i < data2.data.length; i++){
      empNameList.push(data2.data[i].name);
      empAmount.push(data2.data[i].amount);
    }
    option.xAxis.data = empNameList;
    option.series[0].data = empAmount;
    myChart.setOption(option);
    //表格数据
    var flag = true;
    var temp = data2.data[0];
    for(var k = 0; k < data2.data.length - 1; k++){
      for(var j = 0; j < data2.data.length - 1 - k; j++){
        if(data2.data[j].amount < data2.data[j+1].amount){
          temp = data2.data[j];
          data2.data[j] = data2.data[j+1];
          data2.data[j+1] = temp;
          flag = false;
        }
      }
      if(flag){return;}
    }
    for(var l = 0; l < data2.data.length; l++){
      $('#voteTable>tr:nth-of-type('+ (l + 1) +')>td:nth-of-type(2)').text(data2.data[l].name);
      $('#voteTable>tr:nth-of-type('+ (l + 1) +')>td:nth-of-type(3)').text(data2.data[l].amount);
    }
  }
  /*END 获取获取候选人列表*/
});